
module.exports = class extends think.cmswing.ws {
  // 构造方法
  constructor(ctx) {
    super(ctx);
  }
  async __before() {
    await super.__before();
  }
  indexAction() {
    const data = {};
    data.userActive = {
      '描述': '注册通过后回写',
      '接收参数': 'user对象的若干属性',
      '返回参数': 'errno 错误码；errmsg 错误信息；data 返回数据；'
    };
    return this.json(data);
  }

  // 测试用请求
  async testAction() {
    // NC查询示例
    /*
    const params = {
      sql: 'select * from bd_corp',
    };
    const data = await this.requestNC('executeQuery', params);
    */
    /*
    // OA查询示例
    const params = {
      sql: 'select * from formtable_main_89',
    };
    const data = await this.requestOA('executeQuery', params);
    */
    // const data = await this.request('http://localhost:8360/ws/portal/userActive',params);

    const list = await think.model('cmswing/type').getGsName(1);
    console.log('++++++++++++++res++++++++++++++++' + JSON.stringify(list));

    return this.json(list);
  }

  // 基地查询
  async jidiAction() {
    const id = this.get('id');
    let data;
    if (!think.isEmpty(id)) {
      // data =await this.model('document').where({status:1,category_id:130,id:id}).find();
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.status': 1, 'c.id': id}).find();
    } else {
      const categoryId = this.get('category_id');
      if (think.isEmpty(categoryId)) {
        data = await this.model('document').alias('c').join({
          table: 'document_article',
          join: 'left',
          as: 'd',
          on: {'c.id': 'd.id'}
        }).join({
          table: 'ext_attachment_pic',
          join: 'left',
          as: 'e',
          on: {'c.cover_id': 'e.id'}
        }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.category_id': ['in', [130, 150, 151]], 'c.status': 1}).order('c.create_time desc').select();
        if (!data) { return this.fail('查询基地失败') }
        return this.success(data, '查询基地成功');
      } else {
        data = await this.model('document').alias('c').join({
          table: 'document_article',
          join: 'left',
          as: 'd',
          on: {'c.id': 'd.id'}
        }).join({
          table: 'ext_attachment_pic',
          join: 'left',
          as: 'e',
          on: {'c.cover_id': 'e.id'}
        }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.status': 1, 'c.category_id': categoryId}).order('c.create_time desc').select();
      }
    }
    if (!data) { return this.fail('查询基地失败') }
    return this.success(data, '查询基地成功');
  }
  // 园区查询
  async yuanquAction() {
    const id = this.get('id');
    let data;
    if (!think.isEmpty(id)) {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.status': 1, 'c.id': id}).find();
      if (!data) { return this.fail('查询园区失败') }
      return this.success(data, '查询园区成功');
    } else {
      const categoryId = this.get('category_id');
      if (think.isEmpty(categoryId)) {
        data = await this.model('document').alias('c').join({
          table: 'document_article',
          join: 'left',
          as: 'd',
          on: {'c.id': 'd.id'}
        }).join({
          table: 'ext_attachment_pic',
          join: 'left',
          as: 'e',
          on: {'c.cover_id': 'e.id'}
        }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.category_id': ['in', [131, 147, 152]], 'c.status': 1}).order('c.create_time desc').select();
        if (!data) { return this.fail('查询园区失败') }
        return this.success(data, '查询园区成功');
      } else {
        data = await this.model('document').alias('c').join({
          table: 'document_article',
          join: 'left',
          as: 'd',
          on: {'c.id': 'd.id'}
        }).join({
          table: 'ext_attachment_pic',
          join: 'left',
          as: 'e',
          on: {'c.cover_id': 'e.id'}
        }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.status': 1, 'c.category_id': categoryId}).order('c.create_time desc').select();
      }
      if (!data) { return this.fail('查询园区失败') }
      return this.success(data, '查询园区成功');
    }
  }
  // 行情查询 厂家报价-137 市场价格-139 市场分析-140
  async hangqingAction() {
    let data;
    const id = this.get('id');
    const category_id = this.get('category_id');
    const keyname = this.get('keyname');
    if (!think.isEmpty(id)) {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.status': 1, 'c.id': id}).select();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    }
    if (think.isEmpty(category_id)) {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.category_id': ['in', [137, 139, 140]], 'c.status': 1}).order('c.create_time desc').select();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    } else {
      const map = {'c.category_id': category_id, 'c.status': 1};
      if (keyname) {
        map['c.keyname'] = ['like', '%' + keyname + '%'];
      }
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,c.keyname,d.content,e.path').where(map).order('c.create_time desc').select();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    }
  }

  // 资讯查询 行业资讯-142 政策法规-143
  async zixunAction() {
    let data;
    const id = this.get('id');
    const category_id = this.get('category_id');
    if (!think.isEmpty(id)) {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.id': id, 'c.status': 1}).find();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    }
    if (think.isEmpty(category_id)) {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.category_id': ['in', [142, 143]], 'c.status': 1}).order('c.create_time desc').select();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    } else {
      data = await this.model('document').alias('c').join({
        table: 'document_article',
        join: 'left',
        as: 'd',
        on: {'c.id': 'd.id'}
      }).join({
        table: 'ext_attachment_pic',
        join: 'left',
        as: 'e',
        on: {'c.cover_id': 'e.id'}
      }).field('c.id,c.title,c.description,c.category_id,c.update_time,d.content,e.path').where({'c.category_id': category_id, 'c.status': 1}).order('c.create_time desc').select();
      if (!data) { return this.fail('查询资讯失败') }
      return this.success(data, '查询资讯成功');
    }
  }

  async spAction() {
    const id = this.get('id');
    const data = await this.model('category_sp').where({cid: id}).find();
    if (!data) { return this.fail('查询失败') }
    return this.success(data, '查询成功');
  }

  async adAction() {
    const position = this.get('position') || 1;
    const data = await this.model('document').where({position: ['EXP', '&' + position], status: 1}).field('id,title,description,cover_id,link_id').select();
    if (!data) { return this.fail('查询失败') }
    for (let index = 0; index < data.length; index++) {
      const item = data[index];
      item.cover = await get_cover(item.cover_id, 'path');
    }
    return this.success(data, '查询成功');
  }
};
